package com.prs.crm.dao.oa.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.NoticeDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Notice;

public class NoticeDaoImpl extends BaseDaoImpl<Notice> implements NoticeDao{

	@Override
	public Collection<Notice> getAllNotice() {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Notice.class);
		return criteria.list();
	}

	@Override
	public Collection<Notice> getNoticeByIds(Collection<Integer> ids) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Notice.class);
		criteria.add(Restrictions.in("id", ids));
		criteria.addOrder(Order.desc("recordTime"));
		return criteria.list();
	}

	@Override
	public Collection<Notice> getUnreadNotice(Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Notice.class);
		criteria.addOrder(Order.desc("recordTime"));
		criteria.createAlias("accepts", "a");
		criteria.add(Restrictions.eq("a.person", person));
		criteria.add(Restrictions.eq("a.state", Constrains.NOTICE_UNREAD));
		return criteria.list();
	}

	@Override
	public Notice getNoticeId(final Integer id) {
		Criteria criteria = this.getSession().createCriteria(Notice.class);
		criteria.add(Restrictions.eq("id", id));
		List result=criteria.list();
		if (result==null || result.size()==0) {
			return null;
		}
		return (Notice) result.get(0);
	}

}
